﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HQWeb.SysManage.RoleManage
{
    public partial class RoleAccreditEdit : BasePage
    {
        #region Variables

        
        BLL.SysManage.T_PUB_ROLEMENU bll = new BLL.SysManage.T_PUB_ROLEMENU();
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                this.txtIdHid.Text = Request["id"];
                Session["idSign"] = this.txtIdHid.Text.Trim();
                BindListRoleUser();
                BindLabel();
            }
        }

        #region GridView1

        //数据绑定
        private void BindListRoleUser()
        {

            GridView1.DataSource = bll.GetRoleMenu(Session["idSign"].ToString(), "0");
            GridView1.DataBind();
        }

        #endregion

        #region 显示子表数据
        /// <summary>
        /// 显示子表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[1].Visible = false; //隐藏列,可以取得该隐藏列的信息 
            }

            GridViewRow row = e.Row;
            if (row.DataItem == null)
            {
                return;
            }

            GridView gv = new GridView();
            gv = (GridView)row.FindControl("GridView2");
            if (string.IsNullOrEmpty(gv.UniqueID))
            {
                gv.PageIndex = 0;
                gv.EditIndex = -1;

                ClientScript.RegisterStartupScript(GetType(), "Expand", "<SCRIPT LANGUAGE='javascript'>expandcollapse('div" + ((DataRowView)e.Row.DataItem)["mid"].ToString() + "','one');</script>");
            }
            try
            {
                string strPid = Convert.ToString(((DataRowView)e.Row.DataItem)["mid"]);
                DataSet ds = new DataSet();
                ds = bll.GetRoleMenu(Session["idSign"].ToString(), strPid);
                if (ds.Tables[0].Rows.Count>0)
                {
                    gv.DataSource = ds.Tables[0].DefaultView;
                    gv.DataBind(); 
                }
               
            }
            catch (Exception ex)
            {
                Universal.JS.Alert(ex.Message);
                return;
            }

        }

        //protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        //    {
        //        e.Row.Cells[1].Visible = false; //隐藏列,可以取得该隐藏列的信息 
        //    }
        //}

        #endregion

        #region GridView1主表删除操作
        /// <summary>
        /// GridView1主表删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int row = Convert.ToInt32(e.RowIndex);
                int id = Convert.ToInt32(GridView1.Rows[row].Cells[1].Text);

                Model.SysManage.T_PUB_ROLEMENU treeRoleModel = new Model.SysManage.T_PUB_ROLEMENU();
                treeRoleModel = bll.GetModel(id);
                bll.DeleteRoleMenul(Session["idSign"].ToString(), GetNextMenulID(Convert.ToInt32(treeRoleModel.MENU_ID)));
                BindListRoleUser();
            }
            catch (Exception ex)
            {
               // COM.HS.Utility.JScript.Alert(ex.Message);
                return;
            }
        }

        public string  GetNextMenulID(int pID)
        {
            string strids=pID.ToString()+",";
            BLL.SysManage.T_PUB_MENU menuBll = new BLL.SysManage.T_PUB_MENU();
            List<Model.SysManage.T_PUB_MENU> modelList = menuBll.GetnextMenu(pID.ToString(), "");
            foreach (Model.SysManage.T_PUB_MENU item in modelList)
            {
                strids = strids + item.MENU_ID.ToString() + ",";
            }
            return strids.Trim(',');
        }
        #endregion

        #region 子表删除操作
        /// <summary>
        /// GridView3 子表删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                GridView gvTemp = (GridView)sender;
                int row = Convert.ToInt32(e.RowIndex);
                int id = Convert.ToInt32(gvTemp.DataKeys[row].Value);
                bll.Delete(id);
                BindListRoleUser();
            }
            catch (Exception ex)
            {
              Universal.JS.Alert(ex.Message);
                return;
            }
        }
        #endregion

        #region 分页
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            try
            {
                BindListRoleUser();
            }
            catch (Exception ex)
            {
                //COM.HS.Utility.JScript.Alert(ex.Message);
                return;
            }
        }
        #endregion

        #region 填充页面label,写入角色名
        /// <summary>
        /// 填充页面label,写入角色名
        /// </summary>
        protected void BindLabel()
        {
            BLL.SysManage.T_PUB_ROLE roleBll = new BLL.SysManage.T_PUB_ROLE();
            Model.SysManage.T_PUB_ROLE roleModel = new Model.SysManage.T_PUB_ROLE();
            roleModel = roleBll.GetModel(this.txtIdHid.Text.Trim().ToString());
            this.ROLENAME.Text = roleModel.ROLE_NAME;
        }
        #endregion
    }
}